#!/usr/bin/env python
# encoding: utf-8
'''
@author: 王震
@license: 四川凡狼科技有限公司
@contact: 2668985108@qq.com
@file: message.py
@time: 2022/2/16 10:17
@desc:
'''

from main import db
from flask import request
from app.msg.tips import tips, tipsText
from app.public.tool import create_key
from app.msg.resp import response_json, response_list_json
from app.public.sqlMethods import t
from app.model.message.message import Message
from app.model.user.user import User
from app.serialize.message import MessageObj
from app.serialize.user import UserObj


def add_message(data):
    """
    创建用户留言
    :param data:
    :return:
    """
    token = request.headers['token']
    user_token_obj = t.loads(token)
    if not data['info']:
        return response_json(code=tips['parameter_error'], msg=tipsText['parameter_error'])
    message = Message()
    for key in data:
        setattr(message, key, data[key]) if hasattr(message, key) else None
    message.id = create_key()
    message.userId = user_token_obj['id']
    try:
        db.session.add(message)
        db.session.commit()
        return response_json()
    except Exception:
        db.session.rollback()
        return response_json(code=tips['System_error'], msg=tipsText['System_error'])


def get_message(page_number, page_size):
    """
    客户端获取用户留言列表
    :param page_number:  当前页码
    :param page_size:  每一页显示的数据条数
    :return:
    """
    try:
        page = Message.query.filter(Message.shoe == 1).paginate(int(page_number), int(page_size), False)
        item = page.items
        message_obj = MessageObj(many=True)
        data = message_obj.dump(item)
        user_obj = UserObj()
        for items in data:
            res = User.query.filter(User.id == items['userId']).first()
            items['userInfo'] = user_obj.dump(res)
        return response_list_json(data=data, page_number=page.page, page_size=page.pages)
    except Exception:
        db.session.rollback()
        return response_json(code=tips['System_error'], msg=tipsText['System_error'])


def get_system_message(page_number, page_size):
    """
    管理系统获取留言列表
    :param page_number:
    :param page_size:
    :return:
    """
    try:
        page = Message.query.filter().paginate(int(page_number), int(page_size), False)
        item = page.items
        message_obj = MessageObj(many=True)
        data = message_obj.dump(item)
        user_obj = UserObj()
        for items in data:
            res = User.query.filter(User.id == items['userId']).first()
            items['userInfo'] = user_obj.dump(res)
        return response_list_json(data=data, page_number=page.page, page_size=page.pages)
    except Exception:
        db.session.rollback()
        return response_json(code=tips['System_error'], msg=tipsText['System_error'])


def examine_message(data):
    """
    审核留言
    :param data:
    :return:
    """
    if not data['id'] or int(data['show']) < 1 or int(data['show']) > 2:
        return response_json(code=tips['parameter_error'], msg=tipsText['parameter_error'])
    try:
        dic = {"shoe": int(data['show'])}
        res = Message.query.filter(Message.id == data['id']).update(dic)
        if not res:
            return response_json(code=tips['noResult'], msg=tipsText['noResult'])
        return response_json()
    except Exception:
        db.session.rollback()
        return response_json(code=tips['System_error'], msg=tipsText['System_error'])


def del_message(id):
    """
    删除留言
    :param id:
    :return:
    """
    if not id:
        return response_json(code=tips['parameter_error'], msg=tipsText['parameter_error'])
    try:
        res = Message.query.filter(Message.id == id).delete()
        if not res:
            return response_json(code=tips['noResult'], msg=tipsText['noResult'])
        return response_json()
    except Exception:
        db.session.rollback()
        return response_json(code=tips['System_error'], msg=tipsText['System_error'])
